<?php
class admin_util_login{
	const SCOOKIE_NAME = '_admin_session';
	/**
	 * 检查admin模块下session是否存在
	 *
	 * @param int $uid
	 * @return boolean
	 */
	public static function checkSession($uid){
		if(!$_COOKIE[self::SCOOKIE_NAME ]){
			return false;
		}
		$session = Model::factory('admin_model_session')->select($_COOKIE[self::SCOOKIE_NAME ]);
		if($session && $session['uid']==$uid){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 设置session
	 *
	 * @param int $uid
	 * @return boolean
	 */
	public static function setSession($uid){
		$sid = md5(uniqid());
		setcookie(self::SCOOKIE_NAME , $sid, null,'/', DOMAIN);
		$session = Model::factory('admin_model_session');
		$session->remove_uid($uid);
		return $session->write($sid, $uid);
	}
	
	/**
	 * 删除session
	 *
	 * @param int $uid
	 */
	public static function unsetSession($uid){
		$sid = $_COOKIE[self::SCOOKIE_NAME ];
		setcookie(self::SCOOKIE_NAME , '', time()-1,'/', DOMAIN);
		$session = Model::factory('admin_model_session');
		$session->remove_uid($uid);
		$session->remove_sid($sid);
	}
	
	public function is_login(){
		$SL = SecureLogin::getInstance();
		if(!array_key_exists(COOKIE_KEY, $_COOKIE) or empty($_COOKIE[COOKIE_KEY])) {
			return false;
		}
		return $SL->decode_user_cookie();
	}
}